package com.quanquan.user.service;

import com.quanquan.user.entity.CommercialDouble;
import com.quanquan.user.repository.CommercialDoubleRepository;
import com.quanquan.user.request.CustomDouble;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.UUIDGenerator;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @ClassName: commercialDoubleService
 * @Author: xiaoqianh丶
 * @Date: 2019-07-04 14:57
 * @Version: V1.0
 */
@Service
public class CommercialDoubleService {

    private final CommercialDoubleRepository commercialDoubleRepository;

    public CommercialDoubleService(CommercialDoubleRepository commercialDoubleRepository) {
        this.commercialDoubleRepository = commercialDoubleRepository;
    }



    public void createCommercialDouble(List<String> commercialIds,String markingRuleId,Integer num){
        //先把所有的都设为移除
        commercialDoubleRepository.removeShip(markingRuleId);
        for(String commercialId:commercialIds){

            CommercialDouble commercialDouble1 = commercialDoubleRepository.findByCommercialId(commercialId);
            if(commercialDouble1 != null){
                commercialDouble1.setNum(num);
                commercialDouble1.setRemove(0);
                commercialDoubleRepository.saveAndFlush(commercialDouble1);
            }else{
                CommercialDouble commercialDouble = new CommercialDouble();
                commercialDouble.setId(UUIDGenerator.getUUID());
                commercialDouble.setCommercialId(commercialId);
                commercialDouble.setMarkingRulesId(markingRuleId);
                commercialDouble.setCreateTime(DateUtils.currentDate());
                commercialDouble.setRemove(0);
                commercialDouble.setNum(num);
                commercialDoubleRepository.saveAndFlush(commercialDouble);
            }

        }
    }


    /**
     * 创建业态积分翻倍规则
     */
    public void updateCommercialDouble(List<CustomDouble> customDoubles){
        CustomDouble customDouble1 = customDoubles.get(0);
        String markingRuleId = customDouble1.getMarkingRuleId();
        //先把所有的都设为移除
        commercialDoubleRepository.removeShip(markingRuleId);
        for(CustomDouble customDouble : customDoubles){
            String commercialId = customDouble.getCommercialId();
            Integer num = customDouble.getNum();
            CommercialDouble commercialDouble1 = commercialDoubleRepository.findByCommercialId(commercialId);
            if(commercialDouble1 != null){
                commercialDouble1.setNum(num);
                commercialDouble1.setRemove(0);
                commercialDoubleRepository.saveAndFlush(commercialDouble1);
            }else{
                CommercialDouble commercialDouble = new CommercialDouble();
                commercialDouble.setId(UUIDGenerator.getUUID());
                commercialDouble.setCommercialId(commercialId);
                commercialDouble.setMarkingRulesId(markingRuleId);
                commercialDouble.setCreateTime(DateUtils.currentDate());
                commercialDouble.setRemove(0);
                commercialDouble.setNum(num);
                commercialDoubleRepository.saveAndFlush(commercialDouble);
            }

        }
    }


    public List<String> queryCommercialId(String markingRuleId){
        List<CommercialDouble> commercialDoubles = commercialDoubleRepository.findByMarkingRulesIdAndRemove(markingRuleId, 0);
        if(commercialDoubles.size()>0){
            List<String> commercials = commercialDoubles.stream().map(CommercialDouble::getCommercialId).collect(Collectors.toList());
            return commercials;
        }

        return null;
    }

}
